package hot;

import java.util.*;

/**
 * @author way
 * @create 2025-03-04 19:03
 */
public class _438_findAnagrams {
    public List<Integer> findAnagrams(String s, String p) {
        List<Integer> ans = new ArrayList<>();
        char[] sA = s.toCharArray();
        char[] pA = p.toCharArray();
        int[] cntP = new int[26];
        int[] cntS = new int[26];
        for (char c : pA) {
            cntP[c - 'a']++;
        }

        for (int right = 0; right < s.length(); right++) {
            cntS[sA[right] - 'a']++;
            int left = right - p.length() + 1;
            if (left < 0) {
                continue;
            }
            if (Arrays.equals(cntS, cntP)) {
                ans.add(left);
            }
            cntS[sA[left] - 'a']--;
        }

        return ans;
    }
}
